﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace WinHBM.Core.Net.Sockets.Cmd
{
    public class GetUserInfoCmd : SocketCmd
    {
        private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(GetUserInfoCmd));
        public override void executeCmd(JObject request, FlashSocketServer socket)
        {
            FileStream fs=null;
            UserInfo user = null;
            try
            {
                //读取文件流对象 
                fs = new FileStream(Application.StartupPath+"/user.bin", FileMode.Open);
                if (fs.Length > 0)
                {
                    fs.Close();
                    //BinaryFormatter bf = new BinaryFormatter();
                    //读出存在user.bin 里的用户信息
                    //user = bf.Deserialize(fs) as UserInfo;
                    //发送响应数据给Flash

                    user = UserInfo.INSTANCE;

                    JObject requestBody = new JObject();
                    requestBody["user_id"] = user.Id;
                    requestBody["address"] = user.Address;
                    requestBody["mobile"] = user.LoginId;
                    requestBody["password"] = user.Password;
                    requestBody["auto"] = user.Auto.ToString();
                    socket.Send(wrapCmd(CMD_GETUSERINFO, requestBody));
                }
                else {
                    socket.Send(wrapCmd(CMD_GETUSERINFO, "文件读取失败"));
                }
            }
            catch (Exception e)
            {
                socket.Send(wrapCmd(CMD_GETUSERINFO,e.Message));
                Logger.Debug(e);
            }
            finally {
                if (fs != null) {
                    fs.Close();
                }
                
            }
           
            


        }
    }
}
